package expenager.project;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBHistory {
	static final String TAG = "DBHistory";
	static final String DATABASE_NAME = "dbhistory";
	static final int DATABASE_VERSION = 4;
	
	static final String TABLE_HISTORY = "History";
	static final String HIS_DATE = "date";
	static final String HIS_MONTH = "month";
	static final String HIS_YEAR = "year";
	static final String HIS_TYPE = "type";
	static final String HIS_CATEGORY = "category";
	static final String HIS_PAYMENT = "payment";
	static final String HIS_AMOUNT = "amount";
	static final String HIS_COMMENT = "comment";
	
	static final String CREATE_HISTORY = "CREATE TABLE "+TABLE_HISTORY+" ("+HIS_DATE+" VARCHAR,"+HIS_MONTH+" VARCHAR,"+HIS_YEAR+" VARCHAR,"+HIS_TYPE+" VARCHAR,"+HIS_CATEGORY+" VARCHAR,"+HIS_PAYMENT+" VARCHAR,"+HIS_AMOUNT+" DECIMAL(20,2),"+HIS_COMMENT+" VARCHAR)";
	
	final Context context;
	
	DatabaseHelper DBHelper;
	SQLiteDatabase db;
	
	public DBHistory(Context ctx) {
		this.context = ctx;
		DBHelper = new DatabaseHelper(context);
	}
	
	private static class DatabaseHelper extends SQLiteOpenHelper {
		DatabaseHelper (Context context) {
			super(context,DATABASE_NAME,null,DATABASE_VERSION);
		}
		
		@Override
		public void onCreate(SQLiteDatabase db) {
			try {
				db.execSQL(CREATE_HISTORY);
			}
			catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			Log.w(TAG, "Upgrading database from version "+oldVersion+" to "+newVersion+", which will destroy all data");
			db.execSQL("DROP TABLE IF EXISTS contacts");
			onCreate(db);
		}
	}
	
	//---opens the database---
	public DBHistory open() throws SQLException {
		db = DBHelper.getWritableDatabase();
		return this;
	}
	
	//---closes the database---
	public void close() {
		DBHelper.close();
	}
	
	public void addHistory(String date, String month, String year, String type, String category, String payment, double amount, String comment)
	{
		ContentValues initialValues = new ContentValues();
		
		initialValues.put(HIS_DATE, date);
		initialValues.put(HIS_MONTH, month);
		initialValues.put(HIS_YEAR, year);
		initialValues.put(HIS_TYPE, type);
		initialValues.put(HIS_CATEGORY, category);
		initialValues.put(HIS_PAYMENT, payment);
		initialValues.put(HIS_AMOUNT, amount);
		initialValues.put(HIS_COMMENT, comment);
		
		db.insert(TABLE_HISTORY, null, initialValues);
	}
	
	public Cursor getHistory() throws SQLException
	{
		Cursor mCursor = db.query(false, TABLE_HISTORY, new String[] {HIS_DATE,HIS_MONTH,HIS_YEAR,HIS_TYPE,HIS_CATEGORY,HIS_PAYMENT,HIS_AMOUNT,HIS_COMMENT}, null, null, null, null, null, null);
		if(mCursor!=null){
			mCursor.moveToFirst();
		}
		return mCursor;
	}
	
	public Cursor getIncome() throws SQLException
	{
		Cursor mCursor = db.query(false, TABLE_HISTORY, new String[] {HIS_AMOUNT}, HIS_TYPE+"='Income'", null, null, null, null, null);
		if(mCursor!=null){
			mCursor.moveToFirst();
		}
		return mCursor;
	}
	
	public Cursor getExpense() throws SQLException
	{
		Cursor mCursor = db.query(false, TABLE_HISTORY, new String[] {HIS_AMOUNT}, HIS_TYPE+"='Expense'", null, null, null, null, null);
		if(mCursor!=null){
			mCursor.moveToFirst();
		}
		return mCursor;
	}
}
